﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Adp.Pleiade.Fwk.Service.WCF;
using Adp.Pleiade.Fwk.Common;
using System.Xml;
using System.Reflection;
using Adp.Pleiade.Fwk.UnitTest;

namespace OutillageTests
{
    public class AncetreTU : UnitTestService
    {
        public static Dictionary<string, Record> listRecord { get; set; }
        public static Dictionary<string, RecordList> listRecordList { get; set; }

        public static Dictionary<string, XmlNode> dicoNodes { get; set; }

        public static XmlDocument document { get; set; }

        public static string xmlPath { get; set; }
        
        public static void InitialiserObjetsComplexes(string unXML)
        {
            listRecord = new Dictionary<string, Record>();
            listRecordList = new Dictionary<string, RecordList>();
            dicoNodes = new Dictionary<string, XmlNode>();

            xmlPath = unXML;
            document = new XmlDocument();
            document.Load(xmlPath);
            XmlNodeList objectsNodeList = document.DocumentElement.SelectNodes("ObjetsComplexes").Item(0).ChildNodes;

            foreach (XmlNode node in objectsNodeList)
            {
                // Gestion des RecordList
                if (node.Name == "RecordList")
                {
                    var def = new Record();
                    foreach (XmlNode field in node)
                    {
                        if (field.Attributes["fieldName"] != null && field.Attributes["value"] != null)
                            def[Convert.ToString(field.Attributes["fieldName"].Value)] = field.Attributes["value"].Value;
                    }
                    if (def.Count > 0)
                        listRecordList.Add(Convert.ToString(node.Attributes["Value"].Value), new RecordList(def));
                    else
                        listRecordList.Add(Convert.ToString(node.Attributes["Value"].Value), new RecordList());
                }
                // Gestion des Record
                else if (node.Name == "Record")
                {
                    var def = new Record();
                    foreach (XmlNode field in node)
                    {
                        if (field.Attributes["fieldName"] != null && field.Attributes["value"] != null)
                            def[Convert.ToString(field.Attributes["fieldName"].Value)] = field.Attributes["value"].Value;
                    }
                    listRecord.Add(Convert.ToString(node.Attributes["Value"].Value), def);
                }
                // Gestion des IList et IDictionary
                else if (node.Name == "LIST" || node.Name == "DICTIONARY")
                    dicoNodes.Add(node.Attributes["Value"].Value, node);
            }
        }

        // Alimentation des listes
        public static void AlimenterListSelonCle<T>(string uneCle, Type unType, List<T> uneListe)
        {
            try
            {
                // Récupération du noeud associé à la clé, dans le dictionnaire   
                XmlNode baseNode = dicoNodes[uneCle];
                foreach (XmlNode attribute in baseNode)
                {
                    uneListe.Add((T)Convert.ChangeType(attribute.Attributes["Attribute"].Value, typeof(T)));
                }
            }
            catch
            {
                throw new KeyNotFoundException("La List<T> " + uneCle + " n'a pas été trouvée dans " + xmlPath);
            }
        }

        // Alimentation des Dictionnaires
        public static void AlimenterDicoSelonCle<T, U>(string uneCle, Type keyType, Type valueType, Dictionary<T, U> unDico)
        {
            try
            {
                // Récupération du noeud associé à la clé, dans le dictionnaire   
                XmlNode baseNode = dicoNodes[uneCle];
                foreach (XmlNode attribute in baseNode)
                {
                    unDico.Add((T)Convert.ChangeType(attribute.Attributes["Key"].Value, typeof(T)),
                               (U)Convert.ChangeType(attribute.Attributes["Value"].Value, typeof(U)));
                }
            }
            catch
            {
                throw new KeyNotFoundException("Le Dictionary<T, U> " + uneCle + " n'a pas été trouvé dans " + xmlPath);
            }
        }
    }
}
